{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "00dced22-8f81-4a0e-9e55-bfb2be02b09f",
   "metadata": {},
   "outputs": [],
   "source": [
    "###分别引入requests，beautifulsoup库，openpyxl库是对excel文件进行读取操作的###\n",
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "import openpyxl\n",
    "###分别定义三个函数，分别用于获取源代码，获取表格信息并且存入列表ulist，将ulist文件存入excel文件。###\n",
    "def get_html(url):\n",
    "    try:\n",
    "        r=requests.get(url)\n",
    "        r.raise_for_status()\n",
    "        r.encoding=r.apparent_encoding\n",
    "        return r.text\n",
    "    except:\n",
    "        r=\"fail\"\n",
    "        return r\n",
    "def get_contents(ulist,rurl):###rurl是源代码，ulist里存放数据列表\n",
    "    soup=BeautifulSoup(rurl,'html.parser')\n",
    "    trs=soup.find_all('tr')\n",
    "    for tr in trs:\n",
    "        ui=[]\n",
    "        for td in tr:\n",
    "            ts =td.string\n",
    "            ts =ts.strip()\n",
    "            if ts =='':\n",
    "                continue\n",
    "            ui.append(ui)\n",
    "        ulist.append(ui)\n",
    "###这个函数的操作过程是先查找所有的行的内容，然后定义空表格ui，保存行的数据。其中，再定义每一行中的列，并且消除空格，如果是空格跳过，再把内容放到ui里，每循环一次，就把ui数据保存到uilit中###\n",
    "def saveList(ulist):\n",
    "    wb=openpyxl.Workbook()\n",
    "    ws=wb.active\n",
    "    r=1\n",
    "    for line in ulist:\n",
    "        for col in range(1,len(line)+1):\n",
    "            ws.cell(row=r,column=col).value=line[col-1]\n",
    "        r=r+1\n",
    "    wb.save('savetechan.xlsx')\n",
    "###这个函数操作过程是，先读取每行的数据存入line，再存入列，和数据的读取过程类似，最后保存文件起名为savetechan。xlsx###\n",
    "if __name__==\"__main__\":\n",
    "    urli=[]\n",
    "    url=\"https://www.maigoo.com/goomai/278952.html\"\n",
    "    rs=get_html(url)\n",
    "    get_contents(urli,rs)\n",
    "    saveList(urli)\n",
    "###最后，定义空表格urlist，分别启用三个函数。###"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea822608-f93b-418b-b5f5-6d9abdd7c107",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ea80f67-788a-407d-8de5-fe7ae64ddfaf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4b99a0e-b57e-40fa-9167-4dfa047b34a6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7957562d-3e89-4fec-96e6-7443459a1ddf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b74f489f-a873-4558-8ed5-1a78137360a6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b6e4b95-d4e0-4ace-a47d-66ff102789f1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "anaconda-panel-2023.05-py310",
   "language": "python",
   "name": "conda-env-anaconda-panel-2023.05-py310-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
